package com.yanghongyue.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yanghongyue.reggie.bean.Cart;
import com.yanghongyue.reggie.common.BaseContext;
import com.yanghongyue.reggie.common.R;
import com.yanghongyue.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author HongyueYang
 */
@RestController
@RequestMapping("/shoppingCart")
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping("/add")
    public R<Cart> add(@RequestBody Cart cart){
        return R.sucess(cartService.addCart(cart));
    }

    //查询购物车
    @GetMapping("/list")
    public R<List> list(){
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        wrapper.orderByDesc("create_time");
        return R.sucess(cartService.list(wrapper));
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
       QueryWrapper<Cart> wrapper = new QueryWrapper<>();
       wrapper.eq("user_id",BaseContext.getCurrentId());
       return cartService.remove(wrapper) ? R.sucess("清空购物车成功") : R.error("清空购物车失败");
    }

}
